package com.drinks.yiniang.ui.activity.user.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.help.UserTokenSingleton;
import com.drinks.yiniang.common.time.TimeCount;
import com.drinks.yiniang.entity.user.LoginJson;
import com.drinks.yiniang.io.api.CommonApiIO;
import com.drinks.yiniang.io.api.LoginApiIO;
import com.drinks.yiniang.ui.activity.MainActivity;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 绑定手机号页面(快捷登录没绑定手机号的需要绑定)
 *
 * @author han
 * @date 2019/09/03
 */
public class BindPhoneActivity extends BaseTranBarActivity implements OnNavgationBarClickListener, TimeCount.MyCountDownListener {

    //nav
    private NavigationBarLayout nav_bar_bind_phone;
    //账号
    private EditText et_bind_phone_account;
    //验证码
    private EditText et_bind_phone_code;
    //获取验证码
    private Button btn_bind_phone_get_code;
    //确认绑定
    private Button btn_bind_phone_sure;

    //倒计时
    private TimeCount mTimeCount;

    @Override
    protected void initView() {
        nav_bar_bind_phone = findViewById(R.id.nav_bar_bind_phone);

        et_bind_phone_account = findViewById(R.id.et_bind_phone_account);
        et_bind_phone_code = findViewById(R.id.et_bind_phone_code);

        btn_bind_phone_get_code = findViewById(R.id.btn_bind_phone_get_code);
        btn_bind_phone_sure = findViewById(R.id.btn_bind_phone_sure);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mTimeCount = new TimeCount(60000, 1000);//构造CountDownTimer对象
    }

    @Override
    protected void initListener() {
        nav_bar_bind_phone.setOnNavgationBarClickListener(this);
        btn_bind_phone_get_code.setOnClickListener(this);
        btn_bind_phone_sure.setOnClickListener(this);
        mTimeCount.setCountDownListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_bind_phone_get_code://获取验证码
                doGetVerificationCode();
                break;
            case R.id.btn_bind_phone_sure://绑定手机号
                doBindPhone();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onTimerTick(long millisUntilFinished) {
        btn_bind_phone_get_code.setEnabled(false);
        btn_bind_phone_get_code.setText(String.format(getString(R.string.text_get_code_again_format), millisUntilFinished / 1000));
    }

    @Override
    public void onTimerFinish() {
        btn_bind_phone_get_code.setText(getString(R.string.text_get_code_again));
        btn_bind_phone_get_code.setEnabled(true);
    }

    //绑定手机号
    private void doBindPhone() {
        final String mobile = StringUtils.getEditTextString(et_bind_phone_account.getEditableText());

        if (StringUtils.isEmpty(mobile)) {
            ToastUtils.showShort(R.string.text_hint_input_phone_num);
            return;
        }

        if (!StringUtils.isPhone(mobile)) {
            ToastUtils.showShort(R.string.text_hint_input_true_phone_num);
            return;
        }

        String code = StringUtils.getEditTextString(et_bind_phone_code.getText());

        if (StringUtils.isEmpty(code)) {
            ToastUtils.showShort(R.string.text_hint_input_verification_code);
            return;
        }

        showLoadingDialog();

        LoginApiIO.getInstance().doBindPhone(mobile, code, new APIRequestCallback<LoginJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(LoginJson res) {
                UserTokenSingleton.getInstance().setGlobalUserToken(res.getData().getAccess_token());

                SharedPreUtils.getInstance().saveUserToken(res.getData().getAccess_token());
                SharedPreUtils.getInstance().saveUserID(res.getData().getUser().getId());
                SharedPreUtils.getInstance().saveIsOpenPayPasswordID(Constants.STATUS_GLOBAL_YES.equals(res.getData().getUser().getOpen_pay()) ? Constants.STATUS_GLOBAL_YES : Constants.STATUS_GLOBAL_NO);

                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);

                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取验证码
    private void doGetVerificationCode() {
        final String mobile = StringUtils.getEditTextString(et_bind_phone_account.getEditableText());

        if (StringUtils.isEmpty(mobile)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_phone_num));
            return;
        }

        if (!StringUtils.isPhone(mobile)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_true_phone_num));
            return;
        }

        showLoadingDialog();

        CommonApiIO.getInstance().getSmsCode(mobile, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(R.string.text_sending_success);
                //计时开始
                mTimeCount.start();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mTimeCount != null) {
            mTimeCount.setCountDownListener(null);
            mTimeCount.cancel();
            mTimeCount = null;
        }
        super.onDestroy();
    }
}
